﻿package net.wangit.view.unit.grid;

import java.util.ArrayList;
import java.util.List;

import net.wangit.MapList;
import net.wangit.Row;
import net.wangit.context.ActionContext;
import net.wangit.util.Checker;
import net.wangit.view.element.ElementComponent;
import net.wangit.view.element.component.HiddenElement;
import net.wangit.view.unit.UnitComponent;
import net.wangit.view.unit.UnitConfigurationException;
import net.wangit.view.util.Html;


public abstract class Grid {

	protected static Row insertRow;
	String colsConfig[][];
	UnitComponent uc;
	protected Group top;
	protected ArrayList groups;
	private List toolbarElements;

	public Grid(ActionContext ac, UnitComponent uc) {
		groups = new ArrayList();
		toolbarElements = new ArrayList();
		this.uc = uc;
		colsConfig = uc.getColsConfig();
		int column = uc.getColumn();
		top = new Group(null, column);
		String groupsConfig[][] = uc.getGroupsConfig(ac);
		if (uc.getLayoutMode() != 0 && !Checker.isEmpty(groupsConfig)) {
			for (int i = 0; i < groupsConfig.length; i++)
				groups.add(new Group(groupsConfig[i], column));

		}
		setGroups(ac);
	}

	public abstract String write(ActionContext actioncontext, MapList maplist);

	public String writeToolbar(ActionContext ac, MapList data) {
		if (Checker.isEmpty(toolbarElements))
			return null;
		Row row = null;
		if (!Checker.isEmpty(data))
			row = data.getRow(0);
		StringBuffer html = new StringBuffer();
		for (int i = 0; i < toolbarElements.size(); i++)
			((ElementComponent) toolbarElements.get(i))
					.write(ac, html, -1, row);

		return html.toString();
	}

	private void setGroups(ActionContext ac) {
		List hiddenElements = new ArrayList();
		for (int i = 0; i < uc.size(); i++) {
			ElementComponent ec = uc.getElementComponent(i);
			if (ec.getShowMode() != 9)
				if (ec.isToolbarElement())
					toolbarElements.add(ec);
				else if ((ec instanceof HiddenElement) || ec.getShowMode() == 7)
					hiddenElements.add(ec);
				else
					uc.addElement2Group(ac, getGroup(ec.getGroupId()), ec);
		}

		Group group = getGroupWithElements();
		for (int i = 0; i < hiddenElements.size(); i++)
			group.addElement((ElementComponent) hiddenElements.get(i));

	}

	private Group getGroupWithElements() {
		int size = groups.size();
		for (int i = size - 1; i >= 0; i--) {
			Group group = (Group) groups.get(i);
			if (group.size() > 0)
				return group;
		}

		return top;
	}

	private Group getGroup(String groupId) {
		if (Checker.isEmpty(groupId))
			return top;
		for (int i = 0; i < groups.size(); i++) {
			Group group = (Group) groups.get(i);
			if (groupId.equals(group.getGroupId()))
				return group;
		}

		return top;
	}

	protected void beginTable(StringBuffer html) {
		html.append("<table cellpadding=\"0\" width=\"100%\" id=\"").append(
				"u_g_").append(uc.getId()).append("\"");
		if (uc.isShowCellBorder())
			html.append(" cellspacing=\"1\" class=\"").append("DB")
					.append("\"");
		else
			html.append(" cellspacing=\"0\"");
		html.append(">");
		setColsGroup(html);
	}

	protected void setColsGroup(StringBuffer html) {
		try {
			if (colsConfig != null && colsConfig[0].length >= 3) {
				for (int i = 0; i < colsConfig.length; i++) {
					html.append("<COLGROUP");
					String config[] = colsConfig[i];
					Html.setCss(html, config[0]);
					Html.setWidth(html, config[1]);
					Html.setSpan(html, config[2]);
					html.append("></COLGROUP>");
				}

			}
		} catch (Exception e) {
			throw new UnitConfigurationException("cols config error!", uc
					.getDomain(), uc.getId(), "cols config", e);
		}
	}

	static {
		MapList data = new MapList();
		data.put(0, "", "1");
		insertRow = data.getRow(0);
	}
}
